package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextPaint;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.enterprise.ServiceProjectClassEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

public class ServiceProSkuAdapter extends BaseRecycleAdapter<ServiceProjectClassEntity> {

    private ServiceProChildAdapter mAdapter;
    private TextView titleView;
    private List<Long> numberList = new ArrayList<>();
    private List<String> nameList = new ArrayList<>();
    private IDefaultData iDefaultData;

    public ServiceProSkuAdapter(Context context, List<ServiceProjectClassEntity> datas) {
        super(context, R.layout.item_check_service_pro_sku, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final ServiceProjectClassEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        RecyclerView params_list = holder.itemView.findViewById(R.id.params_list);
        mAdapter = new ServiceProChildAdapter(context, entity.getChild());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        params_list.setLayoutManager(gridLayoutManager);
        params_list.setAdapter(mAdapter);
        mAdapter.setItemSelect(true);
        mAdapter.setDefItem(0);
        mAdapter.setOnItemClick(new AdapterOnItemClick<ServiceProjectClassEntity.ChildBean>() {
            @Override
            public void onItemClick(ServiceProjectClassEntity.ChildBean childBean, int position) {
                if (childBean.getName().getBytes().length > 15){
                    MToast.showToast(context,childBean.getName());
                }
                numberList.set(holder.getIndex_position(), childBean.getNo());
                nameList.set(holder.getIndex_position(), childBean.getName());
                iDefaultData.getDefaultData(numberList);
                iDefaultData.getSelectName(nameList);
            }
        });

        titleView = holder.itemView.findViewById(R.id.tv_title);
        TextPaint paint = titleView.getPaint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(1);
        numberList.add(entity.getChild().get(0).getNo());
        nameList.add(entity.getChild().get(0).getName());
        if (holder.getIndex_position() == datas.size() - 1){
            iDefaultData.getDefaultData(numberList);
            iDefaultData.getSelectName(nameList);
        }
    }

    public List<Long> getNumberList() {
        return numberList;
    }

    public List<String> getNameList() {
        return nameList;
    }


    public interface IDefaultData {
        void getDefaultData(List<Long> numberList);

        void getSelectName(List<String> nameList);
    }

    public void onDefaultData(IDefaultData iDefaultData) {
        this.iDefaultData = iDefaultData;
    }
}
